<?php
/**
 * Created by PhpStorm.
 * User: able
 * Date: 2018/1/23
 * Time: 上午9:38
 */

/**
 * Class AliExpress
 * 阿里云快递查询接入
 */
class AliExpress
{
    public $appKey = "24780065";

    public $appSecret = "38f40617bc43ee62b9fd9c58ed4fc5f3";

    public $appCode = "b4c7d190bd0d48349d3e0a3cb91e7c6e";

    public $host = "http://express.woyueche.com";

    public $url = "http://express.woyueche.com/query.action";

    public $expressMap = [
        ["name" => "邮政包裹/平邮", "value" => "youzhengguonei"],
        ["name" => "EMS", "value" => "ems"],
        ["name" => "顺丰", "value" => "shunfeng"],
        ["name" => "申通", "value" => "shentong"],
        ["name" => "圆通", "value" => "yuantong"],
        ["name" => "中通", "value" => "zhongtong"],
        ["name" => "汇通", "value" => "huitongkuaidi"],
        ["name" => "韵达", "value" => "yunda"],
        ["name" => "宅急送", "value" => "zhaijisong"],
        ["name" => "天天", "value" => "tiantian"],
        ["name" => "德邦", "value" => "debangwuliu"],
        ["name" => "国通", "value" => "guotongkuaidi"],
        ["name" => "增益", "value" => "zengyisudi"],
        ["name" => "速尔", "value" => "suer"],
        ["name" => "中铁物流", "value" => "ztky"],
        ["name" => "中铁快运", "value" => "zhongtiewuliu"],
        ["name" => "能达", "value" => "ganzhongnengda"],
        ["name" => "优速", "value" => "youshuwuliu"],
        ["name" => "全峰", "value" => "quanfengkuaidi"],
    ];

    /**
     * @param $express  快递公司
     * @param $trackingNo  快递单号
     * @return mixed
     * @throws Exception
     */
    public function curl($express, $trackingNo)
    {
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $this->appCode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8");
        $bodys = "express=" . $express . "&trackingNo=" . "$trackingNo";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_URL, $this->url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $this->host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);

        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

    public function getExpressMap()
    {
        return $this->expressMap;
    }

}